using Godot;
using System;

public partial class UIManager : Control
{
    [Export]
    public Label ScoreLabel { get; set; }
    
    [Export]
    public Label HealthLabel { get; set; }
    
    [Export]
    public Label LevelLabel { get; set; }
    
    [Export]
    public Control GameOverPanel { get; set; }
    
    [Export]
    public Button RestartButton { get; set; }
    
    public override void _Ready()
    {
        // 订阅游戏管理器事件
        if (GameManager.Instance != null)
        {
            GameManager.Instance.OnScoreChanged += UpdateScore;
            GameManager.Instance.OnHealthChanged += UpdateHealth;
            GameManager.Instance.OnLevelChanged += UpdateLevel;
        }
        
        // 设置重启按钮事件
        if (RestartButton != null)
        {
            RestartButton.Pressed += OnRestartPressed;
        }
        
        // 初始化UI显示
        UpdateScore(0);
        UpdateHealth(100);
        UpdateLevel("Level1");
        
        // 隐藏游戏结束面板
        if (GameOverPanel != null)
        {
            GameOverPanel.Visible = false;
        }
    }
    
    private void UpdateScore(int score)
    {
        if (ScoreLabel != null)
        {
            ScoreLabel.Text = $"分数: {score}";
        }
    }
    
    private void UpdateHealth(int health)
    {
        if (HealthLabel != null)
        {
            HealthLabel.Text = $"生命值: {health}";
        }
        
        // 如果生命值为0，显示游戏结束面板
        if (health <= 0 && GameOverPanel != null)
        {
            GameOverPanel.Visible = true;
        }
    }
    
    private void UpdateLevel(string level)
    {
        if (LevelLabel != null)
        {
            LevelLabel.Text = $"关卡: {level}";
        }
    }
    
    private void OnRestartPressed()
    {
        if (GameManager.Instance != null)
        {
            GameManager.Instance.RestartGame();
        }
        
        // 隐藏游戏结束面板
        if (GameOverPanel != null)
        {
            GameOverPanel.Visible = false;
        }
    }
    
    public override void _ExitTree()
    {
        // 取消订阅事件
        if (GameManager.Instance != null)
        {
            GameManager.Instance.OnScoreChanged -= UpdateScore;
            GameManager.Instance.OnHealthChanged -= UpdateHealth;
            GameManager.Instance.OnLevelChanged -= UpdateLevel;
        }
    }
} 